<!--#
layout("/layouts/platform.html"){
#-->
<style>
    .el-upload input[type=file] {
        display: none;
    }

    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }

    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 128px;
        height: 128px;
        line-height: 128px;
        text-align: center;
    }

    .avatar {
        width: 128px;
        height: 128px;
        display: block;
    }
</style>
<div id="app" v-cloak>
    <el-row class="header navbar bg-white shadow">
        <div class="pull-left offscreen-right mt5">
            <el-button size="medium" @click="openAdd"><i class="ti-plus"></i>新建Banner</el-button>
        </div>
        <div class="pull-right offscreen-right mt5">
            <el-button size="medium" @click="delMore" type="danger" :disabled="multipleSelection.length==0">
                <i class="el-icon-delete"></i>
                删除
            </el-button>
        </div>
    </el-row>
    <el-row class="el-table-container">
        <el-table
                :key="tableKey"
                :data="tableData"
                @sort-change='pageOrder'
                size="small"
                header-align="center"
                :default-sort="{prop: 'opAt', order: 'descending'}"
                style="width: 100%"
                @selection-change="handleSelectionChange"
        >
            <el-table-column
                    type="selection"
                    width="35">
            </el-table-column>

            <el-table-column
                    prop="name"
                    label="名称"
                    header-align="center"
                    align="center"
                    :show-overflow-tooltip="true">
            </el-table-column>

            <el-table-column
                    prop="type"
                    label="类型"
                    header-align="center"
                    align="center"
                    :show-overflow-tooltip="true">
            </el-table-column>

            <el-table-column
                    sortable
                    prop="picture"
                    label="图片"
                    header-align="center"
                    align="center">
                <template slot-scope="scope">
                    <p v-if="scope.row.picture">
                        <img :src="scope.row.picture" style="width: 60px;height: 60px">
                    </p>
                    <p v-else>
                        暂无数据
                    </p>
                </template>
            </el-table-column>

            <el-table-column
                    sortable
                    prop="href"
                    label="跳转URL"
                    align="center"
                    header-align="center">
            </el-table-column>

            <el-table-column label="启用状态" header-align="center" prop="disabled" align="center"
                             :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    <i v-if="scope.row.disabled"
                       class="fa fa-circle text-danger ml5"></i>
                    <i v-if="!scope.row.disabled"
                       class="fa fa-circle text-success ml5"></i>
                </template>
            </el-table-column>

            <el-table-column
                    prop="userOnline"
                    label="操作"
                    width="120"
                    align="center">
                <template slot-scope="scope">
                    <el-dropdown @command="dropdownCommand">
                        <el-button size="mini">
                            <i class="ti-settings"></i>
                            <span class="ti-angle-down"></span>
                        </el-button>
                        <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item divided :command="{type:'enable',id:scope.row.id,row:scope.row}">
                                启用
                            </el-dropdown-item>
                            <el-dropdown-item :command="{type:'disable',id:scope.row.id,row:scope.row}">
                                禁用
                            </el-dropdown-item>
                            <el-dropdown-item divided :command="{type:'edit',id:scope.row.id}">
                                修改
                            </el-dropdown-item>
                            <el-dropdown-item :command="{type:'delete',id:scope.row.id,name:scope.row.name}"
                                              v-if="scope.row.name!='superadmin'">
                                删除
                            </el-dropdown-item>
                        </el-dropdown-menu>
                    </el-dropdown>
                </template>
            </el-table-column>
        </el-table>
    </el-row>
    <!--    分页-->
    <el-row class="el-pagination-container">
        <el-pagination
                @size-change="pageSizeChange"
                @current-change="pageNumberChange"
                :current-page="pageForm.pageNumber"
                :page-sizes="[10, 20, 30, 50]"
                :page-size="pageForm.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="pageForm.totalCount">
        </el-pagination>
    </el-row>

    <!--    新建/修改 Banner弹窗-->
    <el-dialog
            :title="titlePopu==0?'新建BANNER':'修改BANNER'"
            :visible.sync="addDialogVisible"
            :close-on-click-modal="false"
            width="40%">
        <el-form :model="formData" ref="addForm" :rules="formRules" size="small" label-width="200px">
            <!--输入名称-->
            <el-form-item prop="name" label="名称">
                <el-input maxlength="100" placeholder="名称"
                          v-model="formData.name"
                          auto-complete="off" tabindex="2" type="text"></el-input>
            </el-form-item>

            <!--类型-->
            <el-form-item prop="type" label="类型" maxlength="20">
                <el-select v-model="formData.type" placeholder="请选择类型" auto-complete="off" tabindex="3">
                    <el-option v-for="item in options" :label="item" :value="item"></el-option>
                </el-select>
            </el-form-item>

            <!-- 上传图片 -->
            <el-form-item prop="picture" label="图片">
                <!--                <p v-if="formData.picture">-->
                <!--                    <img :src="formData.picture" style="width: 60px;height: 60px">-->
                <!--                </p>-->
                <!--                <p v-else>-->
                <!--                    暂无数据-->
                <!--                </p>-->
                <!--                <input type="file" class="file" name="avatar" ref="avatarImg" accept="image/png, image/jpeg"-->
                <!--                       @change="changeUpdate">-->

                <el-upload
                        class="avatar-uploader"
                        tabindex="5"
                        action="${base}/open/file/upload/image2"
                        name="Filedata"
                        :before-upload="beforeUpload"
                        :show-file-list="false"
                        :on-success="function(resp,file,fileList){return handlePicSuccess(resp,file,fileList,'picture')}">
                    <img v-if="formData.picture" :src="formData.picture" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>

            </el-form-item>

            <!-- 跳转URL -->
            <el-form-item prop="href" label="跳转">
                <el-input placeholder="跳转"
                          v-model="formData.href"
                          auto-complete="off" tabindex="6" type="text"></el-input>
            </el-form-item>

            <!-- 启用状态 -->
            <el-form-item prop="disabled" label="启用状态">
                <el-switch
                        v-model="formData.disabled"
                        active-color="#ff4949"
                        inactive-color="#13ce66">
                </el-switch>
            </el-form-item>
        </el-form>
        <!-- 提交按钮 -->
        <span slot="footer" class="dialog-footer">
                    <el-button @click="addDialogVisible = false">取 消</el-button>
                    <el-button type="primary" @click="doAdd">确 定</el-button>
                  </span>
    </el-dialog>
</div>
<script>
    new Vue({
        el: "#app",
        data: function () {
            var self = this;
            return {
                options: ["Phone", "PC", "Crown"], // 复用。有新类型直接添加进数组。
                titlePopu: 0,
                addDialogVisible: false,
                editDialogVisible: false,
                roleDialogVisible: false,
                tableData: [],
                tableKey: "",
                parentUnit: [],
                parentUnitSearch: [],
                pageForm: {
                    searchUnit: "",
                    searchName: "",
                    searchKeyword: "",
                    pageNumber: 1,
                    pageSize: 10,
                    totalCount: 0,
                    pageOrderName: "",
                    pageOrderBy: ""
                },
                formData: {
                    name: "",
                    href: "",
                    type: "",
                    picture: "",
                    disabled: false
                },
                formRules: {
                    name: [
                        {required: true, message: '请输入名称', trigger: 'blur'}
                    ],
                    type: [
                        {required: true, message: '类型错误', trigger: 'blur'}
                    ],
                    picture: [
                        {required: true, message: '请上传图片', trigger: 'blur'}
                    ]
                },
                roleDialogTitle: "查看用户权限",
                roleTableData: [],//treeTable后台取出的数据
                userId: "",//当前用户ID
                multipleSelection: [],
                props: {
                    lazy: true,
                    checkStrictly: true,
                    multiple: false,
                    lazyLoad: function (node, resolve) {
                        var url = base + "/platform/sys/unit/tree";
                        $.post(url, {pid: node.value}, function (data) {
                            if (data.code === 0) {
                                resolve(data.data);
                            }
                        }, "json");
                    }
                }

            }
        },
        methods: {
            beforeUpload(file) {
                console.log(file)
                var testmsg = file.name.substring(file.name.lastIndexOf('.') + 1)
                const extension = testmsg === 'png'
                const extension2 = testmsg === 'jpg'
                // const isLt2M = file.size / 1024 / 1024 < 10
                if (!extension && !extension2) {
                    this.$message({
                        message: '只能上传图片文件!',
                        type: 'warning'
                    });
                }
                // if(!isLt2M) {
                //     this.$message({
                //         message: '上传文件大小不能超过 10MB!',
                //         type: 'warning'
                //     });
                // }
                // return (extension || extension2) && isLt2M
                return extension || extension2
            },
            imgSrcFun: function (src) {
                return base + src;
            },
            changeUpdate: function () {
                var _this = this;
                var fileData = this.$refs.avatarImg.files[0];
                var formDataImg = new FormData();
                formDataImg.append('Filedata', fileData);
                $.ajax({
                    type: "POST",
                    url: base + "/open/file/upload/image2",
                    data: formDataImg,
                    processData: false, // 告诉jQuery不要去处理发送的数据
                    contentType: false, // 告诉jQuery不要去设置Content-Type请求头
                    success: function (res) {
                        if (res.code === 0) {
                            _this.$message({
                                message: res.msg,
                                type: 'success'
                            });
                            _this.$set(_this.formData, 'picture', res.data);//返回路径
                        } else {
                            _this.$message({
                                message: res.msg,
                                type: 'error'
                            });
                        }
                    },
                    error: function (data) {
                        _this.$message({
                            message: res.msg,
                            type: 'error'
                        });
                    }
                });
            },
            pageOrder: function (column) {//按字段排序
                this.pageForm.pageOrderName = column.prop;
                this.pageForm.pageOrderBy = column.order;
                this.pageData();
            },
            pageNumberChange: function (val) {//页码更新操作
                this.pageForm.pageNumber = val;
                this.pageData();
            },
            pageSizeChange: function (val) {//分页大小更新操作
                this.pageForm.pageSize = val;
                this.pageData();
            },
            pageData: function () {//加载分页数据
                var base = "${base!}";
                var self = this;
                sublime.showLoadingbar();//显示loading
                $.post(base + "/platform/cms/banner/data", self.pageForm, function (data) {
                    sublime.closeLoadingbar();//关闭loading
                    if (data.code === 0) {
                        self.tableData = data.data.list;
                        self.pageForm.totalCount = data.data.totalCount;
                    } else {
                        self.$message({
                            message: data.msg,
                            type: 'error'
                        });
                    }
                }, "json");
            },
            formatAt: function (val) {
                if (val > 0)
                    return moment(val * 1000).format("YYYY-MM-DD HH:mm");
                return "";
            },
            doSearch: function () {
                var unit = this.parentUnitSearch[this.parentUnitSearch.length - 1];
                if ("root" === unit) {
                    unit = "";
                }
                this.pageForm.searchUnit = unit;
                this.pageForm.pageNumber = 1;
                this.pageData();
            },
            // 新建/修改Banner按钮逻辑
            openAdd: function () {
                var self = this;
                self.titlePopu = 0;
                this.addDialogVisible = true;
                //打开新增窗口,表单先清空
                this.formData = {
                    type: this.options[0] // 类型默认值Phone
                };
                if (this.$refs["addForm"]) {
                    this.$refs["addForm"].resetFields();
                }
            },
            // 提交按钮逻辑
            doAdd: function () {
                var self = this;
                self.$refs["addForm"].validate(function (valid) {
                    if (valid) {
                        //titlePopu=0是新增,1是修改
                        if (self.titlePopu === 0) {
                            $.post(base + "/platform/cms/banner/add", self.formData, function (data) {
                                if (data.code === 0) {
                                    self.$message({
                                        message: data.msg,
                                        type: 'success'
                                    });
                                    self.parentUnitSearch = [];
                                    self.doSearch();//查询是带条件的,加载新建用户选择的单位用户数据
                                    //self.pageData();
                                    self.addDialogVisible = false;
                                } else {
                                    self.$message({
                                        message: data.msg,
                                        type: 'error'
                                    });
                                }
                            }, "json");
                        } else {
                            $.post(base + "/platform/cms/banner/editDo", self.formData, function (data) {
                                if (data.code === 0) {
                                    self.$message({
                                        message: data.msg,
                                        type: 'success'
                                    });
                                    self.parentUnitSearch = [];
                                    self.doSearch();//查询是带条件的,加载新建用户选择的单位用户数据
                                    self.addDialogVisible = false;
                                } else {
                                    self.$message({
                                        message: data.msg,
                                        type: 'error'
                                    });
                                }
                            }, "json");
                        }
                    }
                });
            },
            dropdownCommand: function (command) {//监听下拉框事件
                var self = this;
                // 禁用/启用 Banner
                if ("enable" === command.type || "disable" === command.type) {
                    $.post(base + "/platform/cms/banner/" + command.type + "/" + command.id, {}, function (data) {
                        if (data.code === 0) {
                            self.$message({
                                message: data.msg,
                                type: 'success'
                            });
                            if ("disable" === command.type) {
                                command.row.disabled = true;
                            }
                            if ("enable" === command.type) {
                                command.row.disabled = false;
                            }
                        } else {
                            self.$message({
                                message: data.msg,
                                type: 'error'
                            });
                        }
                    }, "json");
                }
                // 修改Banner按钮
                if ("edit" === command.type) {
                    $.post(base + "/platform/cms/banner/edit/" + command.id, {}, function (data) {
                        if (data.code === 0) {
                            if (self.$refs["addForm"]) {// 清空校验提示
                                self.$refs["addForm"].resetFields();
                            }
                            self.formData = data.data;//加载后台表单数据
                            self.addDialogVisible = true;//打开编辑窗口
                            self.titlePopu = 1;
                        } else {
                            self.$message({
                                message: data.msg,
                                type: 'error'
                            });
                        }
                    }, "json");
                }
                // 删除Banner按钮
                if ("delete" === command.type) {
                    self.$confirm('此操作将删除 ' + command.name, '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning',
                        callback: function (a, b) {
                            if ("confirm" === a) {//确认后再执行
                                $.post(base + "/platform/cms/banner/delete/" + command.id, {}, function (data) {
                                    if (data.code === 0) {
                                        self.$message({
                                            message: data.msg,
                                            type: 'success'
                                        });
                                        self.doSearch();
                                    } else {
                                        self.$message({
                                            message: data.msg,
                                            type: 'error'
                                        });
                                    }
                                }, "json");
                            }
                        }
                    });
                }
            },
            delMore: function () {
                var self = this;
                if (self.multipleSelection.length === 0) {
                    self.$message({
                        message: "请选择消息",
                        type: 'warning'
                    });
                    return false;
                }
                var ids = [];
                self.multipleSelection.forEach(function (val) {
                    ids.push(val.id);
                });
                self.$confirm('您确定要删除选中的 ' + ids.length + ' 条消息？ ', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                    callback: function (a, b) {
                        if ("confirm" === a) {//确认后再执行
                            $.post(base + "/platform/cms/banner/delete", {ids: ids.toString()}, function (data) {
                                if (data.code === 0) {
                                    self.$message({
                                        message: data.msg,
                                        type: 'success'
                                    });
                                    self.pageData();
                                } else {
                                    self.$message({
                                        message: data.msg,
                                        type: 'error'
                                    });
                                }
                            }, "json");
                        }
                    }
                });
            },
            // 表头 勾选/取消
            handleSelectionChange: function (val) {
                this.multipleSelection = val;
            },
            // 图片上传
            handlePicSuccess: function (response, file, fileList, attrName) {
                if (response.code === 0) {
                    // 对象属性直接赋值不会触发试图更新，采用 Vue.set 、this.$set或者Object.assign({}，this.obj)创建新对象
                    // 或者也可以定义一个 refresh 属性 ，当数据发生改变时，该属性同时变化，也会触发视图更新
                    // this.formData.site_logo =response.data;
                    this.$set(this.formData, attrName, response.data);
                } else {
                    this.$set(this.formData, attrName, "");
                }
            }
        },
        created: function () {
            this.pageData();
        }
    });
</script>
<!--#
}
#-->